home *** CD-ROM | disk | FTP | other *** search
- Path: news.smartlink.net!usenet
- From: thomas@smartlink.net (tom)
- Newsgroups: comp.lang.c++
- Subject: help w/ operator overloading
- Date: 20 Jan 1996 07:48:02 GMT
- Organization: none
- Message-ID: <4dq6ni$nrq@frodo.smartlink.net>
- NNTP-Posting-Host: pm35.smartlink.net
- X-Newsreader: Cyberjack News 7.00
-
- Help,
- Anyone know why I don't get into my XPoint + operator when adding the classes
- together? The Z works but not the X & Y.
-
- Thanks
- ==============================================================================
-
- #include <stdio.h>
-
- class XPoint {
- private:
- double x,y;
-
- public:
- double & X(){ return x;}
- double & Y(){ return y;}
-
- XPoint(double _x, double _y):x(_x),y(_y){}
- XPoint():x(0.0),y(0.0){}
-
- virtual XPoint operator+( XPoint &pt){
- x = x + pt.X();
- y = y + pt.Y();
- return *this;
- }
-
- virtual XPoint operator=( XPoint &pt){
- x = pt.X();
- y = pt.Y();
- return *this;
- }
-
- };
-
-
- class XPoint3D: virtual public XPoint{
- private:
- double z;
- public:
- double &Z(){ return z;}
- XPoint3D(double _x, double _y, double _z):XPoint(_x,_y),z(_z){}
- XPoint3D():XPoint(),z(0.0){}
-
- XPoint3D operator+( XPoint3D &pt){
- Z() = Z() + pt.Z();
- return *this;
- }
- XPoint3D operator=( XPoint3D &pt){
- Z() = pt.Z();
- return *this;
- }
- };
-
-
-
- void main(){
-
- XPoint3D a;
- XPoint3D b(10,10,10);
- XPoint3D c(10,10,10);
-
- printf( "a= (%f,%f)\n",a.X(),a.Y() );
- printf( "b= (%f,%f)\n",b.X(),b.Y() );
- printf( "c= (%f,%f)\n",c.X(),c.Y() );
-
- a = (b + c);
-
- printf("\n\n");
- printf( "a= (%f,%f)\n",a.X(),a.Y() );
- printf( "b= (%f,%f)\n",b.X(),b.Y() );
- printf( "c= (%f,%f)\n",c.X(),c.Y() );
-
- double x,y,z;
- y=z=10.0;
- x=y+z;
- printf("\n\nx=%f,y=%f,z=%f\n",x,y,z);
- }
-
- Output:
- a=0,0
- b=10,10
- c=10,10
-
- a=0,0
- b=10,10
- c=10,10
-
-